package com.chenyanqing.model.two.iocContxt;

import com.chenyanqing.model.two.annotation.Service;
import com.chenyanqing.model.two.factory.BeanFactory;
import com.chenyanqing.model.two.factory.TimeLogAnnotationProxyFactory;
import com.chenyanqing.model.two.utils.XMLBeanConfigParser;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.io.Resources;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import java.io.InputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class ServiceAnnotationApplicationContext {

    private static Logger logger = LoggerFactory.getLogger(ServiceAnnotationApplicationContext.class);


    private ServiceAnnotationApplicationContext(){

    }


    private static final ServiceAnnotationApplicationContext context = new ServiceAnnotationApplicationContext();

    public static ServiceAnnotationApplicationContext getInstance(){
        return context;
    }


    public Object getBean(String className){
        return BeanFactory.getBeanFactory().getBeanById(className);
    }

    public void  initApplicationContext(String beanConfigResource) throws Exception {

        InputStream resourceAsSteam = Resources.getResourceAsStream("spring-definition-mvc.xml");
        logger.info("项目开始启动，加载配置文件:spring-definition-mvc.xml");
        Set<String> beanClassName = XMLBeanConfigParser.parseBeanScanner(resourceAsSteam);

        Map<String,String> needInitIocServiceBean = needInitServiceBean(beanClassName);

        logger.info("初始化bean start...");
        if(CollectionUtils.isEmpty(needInitIocServiceBean)){
            return;
        }
        else{
            for (Map.Entry<String,String> entry:needInitIocServiceBean.entrySet()) {
                logger.info("初始化bean，创建@service注解的bean到Ioc容器:{}",entry.toString());
                BeanFactory.getBeanFactory().createBean(entry.getKey(),entry.getValue());
            }

        }
        logger.info("初始化bean end...");

    }


    /**
     * key是beanclassname：com.chenyanqing.model.two.factory
     * value是 bean map池中的id'值，默认就是等于beanclassname，如果注解配置了value值，则改为注解的值
     * @param beanClassName
     * @return
     * @throws Exception
     */
    private Map<String,String> needInitServiceBean(Set<String> beanClassName) throws Exception {
        if(CollectionUtils.isEmpty(beanClassName)){
            return  new HashMap<>();
        }else{
            Map<String,String> map = new HashMap<>();
            Set<String> set = new HashSet<>();
            for (String s : beanClassName) {
                Class<?> aClass = Class.forName(s);
                Service annotation = aClass.getAnnotation(Service.class);
                if(annotation!=null) {
                    if(StringUtils.isNotBlank(annotation.value())){
                        map.put(s,annotation.value());
                    }else{
                        map.put(s,s);
                    }
                }

            }
            return map;
        }
    }
}
